package service

import (
	"fmt"
	"my-systems/pet-system/internal/info"
	"my-systems/pet-system/internal/models"
	"my-systems/pet-system/internal/sql/foster_sql"
	"my-systems/pet-system/internal/sql/pet_sql"
	"my-systems/pet-system/pkg/e"
	"net/http"
	"strconv"
)

type Foster struct {
	// 用户 ID，关联用户表
	UserID uint `form:"user_id" json:"user_id"`
	// 宠物 ID，关联宠物表
	PetID uint `form:"pet_id" json:"pet_id"`
	// 寄养地点
	Location string `form:"location" json:"location"`
	// 寄养状态，如待审核、已确认、已完成、已取消等
	Status string `form:"status" json:"status"`
	// 寄养备注信息
	Remark string `form:"remark" json:"remark"`
	// 寄养费用
	Cost float64 `form:"cost" json:"cost"`
}

func (f *Foster) AddFoster(uid int) info.Response {
	code := http.StatusOK
	// 检查用户是否有该宠物
	_, exist1, err := pet_sql.ExistOrNotByID(f.PetID, uid)
	if !exist1 || err != nil {
		code := e.ThePetNotExists
		return info.Response{
			Status: code,
			Msg:    e.GetMsgInfo(code),
			Data:   "用户无该宠物",
			Error:  err.Error(),
		}
	}
	// 用户名下有宠物存在则，检查是否有寄养记录，不可重复寄养
	exist2, err := foster_sql.CheckFoster(uid, f.PetID)
	if err != nil {
		code = http.StatusInternalServerError
		return info.Response{
			Status: code,
			Msg:    "查询寄养记录失败" + err.Error(),
		}
	}
	if exist2 {
		code := e.ThePetHasAlreadyBeenFostered
		return info.Response{
			Status: code,
			Msg:    e.GetMsgInfo(code),
		}
	}
	foster := &models.Foster{
		UserID:   uint(uid),
		PetID:    f.PetID,
		Location: f.Location,
		Remark:   f.Remark,
		Cost:     f.Cost,
	}
	err = foster_sql.AddFoster(foster)
	if err != nil {
		code = http.StatusInternalServerError
		return info.Response{
			Status: code,
			Msg:    "添加寄养记录失败" + err.Error(),
		}
	}
	return info.Response{
		Status: code,
		Msg:    "添加寄养记录成功",
	}
}

func (f *Foster) UpdateFoster(uid int) info.Response {
	foster := &models.Foster{
		Location: f.Location,
		Remark:   f.Remark,
	}
	code := http.StatusOK
	// 检查是否有寄养信息
	exist, err := foster_sql.CheckFoster(uid, f.PetID)
	if err != nil {
		code = http.StatusInternalServerError
		return info.Response{
			Status: code,
			Msg:    "查询寄养记录失败" + err.Error(),
		}
	}
	fmt.Println(uid)
	if !exist {
		code = e.ThePetNotExists
		return info.Response{
			Status: code,
			Msg:    e.GetMsgInfo(code),
		}
	}
	// 存在寄养信息则进行更新
	err = foster_sql.UpdateFoster(uid, f.PetID, foster)
	if err != nil {
		code = http.StatusInternalServerError
		return info.Response{
			Status: code,
			Msg:    "更新寄养记录失败" + err.Error(),
		}
	}
	return info.Response{
		Status: code,
		Msg:    "更新寄养记录成功",
	}
}

func (f *Foster) DeleteFosterByID(uid int, id string) info.Response {
	idInt, _ := strconv.Atoi(id)
	code := http.StatusOK
	// 检查是否有寄养信息 -- 用户ID,预约ID
	exist, err := foster_sql.CheckFosterByID(uid, idInt)
	if err != nil {
		code = http.StatusInternalServerError
		return info.Response{
			Status: code,
			Msg:    "查询寄养记录失败" + err.Error(),
		}
	}
	if !exist {
		code = e.TheAppointmentRecordDoesNotExist
		return info.Response{
			Status: code,
			Msg:    e.GetMsgInfo(code),
		}
	}

	// 删除寄养信息
	err = foster_sql.DeleteForster(uid, uint(idInt))
	if err != nil {
		code = e.DeletingFosterCareInformationFailed
		return info.Response{
			Status: code,
			Msg:    e.GetMsgInfo(code),
			Error:  err.Error(),
		}
	}
	return info.Response{
		Status: code,
		Msg:    e.GetMsgInfo(code),
	}
}

func (f *Foster) GetFosters(uid int) info.Response {
	code := http.StatusOK
	fosters, err := foster_sql.GetFosters(uid)
	if err != nil {
		code = http.StatusInternalServerError
		return info.Response{
			Status: code,
			Msg:    "查询寄养记录失败" + err.Error(),
		}
	}
	return info.Response{
		Status: code,
		Data:   fosters,
		Msg:    "查询寄养记录成功",
	}
}

func (f *Foster) GetFosterByID(uid int, id string) info.Response {
	idInt, _ := strconv.Atoi(id)
	code := http.StatusOK

	exist, err2 := foster_sql.CheckFosterByID(uid, idInt)
	if err2 != nil {
		code = http.StatusInternalServerError
		return info.Response{
			Status: code,
			Msg:    "查询寄养记录失败" + err2.Error(),
		}
	}
	if !exist {
		code = e.TheAppointmentRecordDoesNotExist
		return info.Response{
			Status: code,
			Msg:    e.GetMsgInfo(code),
		}
	}

	fosters, err := foster_sql.GetFosterByID(uid, idInt)
	if err != nil {
		code = http.StatusInternalServerError
		return info.Response{
			Status: code,
			Msg:    "查询寄养记录失败" + err.Error(),
		}
	}
	return info.Response{
		Status: code,
		Data:   fosters,
		Msg:    "查询寄养记录成功",
	}
}
